import ins from "../api/ins/list";
import { getStore } from "../utils/tools";
import store from "../vuex/store";
import Vue from "vue";
import Vuex from "vuex";
import { timeProcessDay } from "../utils/tools";

export const validation = {
  // 非空校验
  require: (rule, value, callback) => {
    if (value === "" || value === null || value === undefined) {
      callback(new Error("这个必须要填哦！"));
    } else {
      callback();
    }
  },
  // 非空校验 提示文字字数控制
  requireThree: (rule, value, callback) => {
    if (value === "" || value === null || value === undefined) {
      callback(new Error("必填项！"));
    } else {
      callback();
    }
  },
   //籍贯地址非空校验
  addressRequire: (rule, value, callback) => {
    let len = value.length || '';
    if (len == 0) {
      callback(new Error("这个必须要选哦！"));
    } else {
      callback();
    }
  },
    // 字数长度校验，不超过15字
  inpLengthRule15: (rule, value, callback) => {
    let len = value.length;
    if (len > 15) {
      callback(new Error("不超过15个字"));
    } else {
      callback();
    }
  },
    // 字数长度校验，不超过32字
  inpLengthRule: (rule, value, callback) => {
     if (value === "" || value === null || value === undefined) {
        callback();
     }else{
        let len = value.length;
      if (len > 32) {
        callback(new Error("不超过32个字"));
      } else {
        callback();
      }
  }
  },
  // 字数长度校验，不超过64字
  inpLengthRule64: (rule, value, callback) => {
     if (value === "" || value === null || value === undefined) {
        callback();
     }else{
        let len = value.length;
      if (len > 64) {
        callback(new Error("不超过64个字"));
      } else {
        callback();
      }
  }
  },
  // 长文本字数校验不超过512字
  LongTextRule: (rule, value, callback) => {
    let len = value.length || "";
    if (len > 512) {
      callback(new Error("不超过512个字"));
    } else {
      callback();
    }
  },
  // 审批意见字数长度校验，不超过250字
  opinonMsgRule: (rule, value, callback) => {
    if(value === undefined || value === null){
       callback();
    }else{
        let len = value.length || "";
       if (len > 250) {
        callback(new Error("不超过250个字（包括空格、回车、标点符号、特殊字符）"));
      } else {
        callback();
      }
    }
  },
    // 字数长度校验，不超过200字
  opinonMsgRule200: (rule, value, callback) => {
    if(value === undefined || value === null){
      callback();
    }else{
      let len = value.length || "";
      if (len > 200) {
        callback(new Error("不超过200个字（包括空格、回车、标点符号、特殊字符）"));
      } else {
        callback();
      }
    }
  },
	// 审批意见字数长度校验，不超过100字
	opinonMsgRule100: (rule, value, callback) => {
		if(value === undefined || value === null){
			callback();
		}else{
			let len = value.length || "";
			if (len > 100) {
				callback(new Error("不超过100个字（包括空格、回车、标点符号、特殊字符）"));
			} else {
				callback();
			}
		}
	},
  // 字数长度校验，不超过15字
  nameLengthRule: (rule, value, callback) => {
    if (value === "" || value === null || value === undefined) {
         callback();
    }else{
       let len = value.length || "";
        if (len > 15) {
          callback(new Error("不超过15个字"));
        } else {
          callback();
        }
     }
  },
  // 险种名称长度校验，不超过20字
  insurNameLengthRule: (rule, value, callback) => {
    let len = value.length || "";
    if (len > 12) {
      callback(new Error("不超过12个字"));
    } else {
      callback();
    }
  },
  // 岗位薪资
  postSalLength: (rule, value, callback) => {
    if (value !== "" && value !== null &&value !== undefined && !/^(0|[1-9]\d{0,4})$/.test(value)) {
      callback(new Error("最多5位整数"));
    } else {
      callback();
    }
  },
  // 后台请求校验
  validRule1: (rule, value, callback) => {
    ins
      .vaildCTrl("/app-hr/api/insurance/insurance/list/", {
        pageNo: 2,
        pageSize: 10,
        orderField: "createTime",
        orderSort: "DESC",
        searchParameters: "1123"
      })
      .then(res => {
        if (res.success == "0") {
          callback();
        } else {
          callback(new Error(res.msg));
        }
      })
      .catch(err => {
        console.log(err);
      });
  },
  // 身份证合法验证/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\w)$/
  // //(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])
  isIdCardNo: (rule, value, callback) => {
    if (
      !/^(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)$/.test(
        value
      )
    ) {
      callback(new Error("请输入正确的身份证号码!"));
    } else {
      callback();
    }
  }, // 邮编验证
  isZipCode: (rule, value, callback) => {
    if (!/^[0-9]{6}$/.test(value)) {
      callback(new Error("请正确填写邮政编码!"));
    } else {
      callback();
    }
  }, // 电话号码合法验证 /^1\d{10}$/  /^1[3|4|5|8][0-9]\d{4,8}$/ || value.length != 11
  isMobile: (rule, value, callback) => {
    if (!/^(1[3567984]\d{9})$/.test(value)) {
      callback(new Error("请正确填写手机号码!"));
    } else {
      callback();
    }
  }, //银行卡号合法16-19位
  isBackId: (rule, value, callback) => {
    if (!/^([1-9]{1})(\d{15}|\d{18})$/.test(value)) {
      callback(new Error("请正确填写您的银行卡号!"));
    } else {
      callback();
    }
  }, // 中文汉字验证
  isChineseCharacters: (rule, value, callback) => {
    if (!/^[\u4E00-\u9FA5]{0,}$/.test(value)) {
      callback(new Error("内容只能包括中文文字!"));
    } else {
      callback();
    }
  }, // 排除特殊字符验证
  isChineseCharacters: (rule, value, callback) => {
    if (
      new RegExp(
        "[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）|{}【】‘；：”“'。，、？%+———— ]"
      ).test(value)
    ) {
      callback(new Error("内容不能出现特殊字符!"));
    } else {
      callback();
    }
  }, // （1~4）位非负整数验证
  nonnegativeInteger: (rule, value, callback) => {
    if (!/^\d{1,4}$/.test(value)) {
      callback(new Error("输入内容必须为(1~4)位非负整数！"));
    } else {
      callback();
    }
  },
     // 5位数字验证
  userNumber: (rule, value, callback) => {
    if (value !== ""&&value !== null &&value !== undefined &&!/^\d{5}$/.test(value)) {
      callback(new Error("工号为5位数字！"));
    } else {
      callback();
    }
  },
   // 10位数字验证
  isTenDigit: (rule, value, callback) => {
    if (!/^\d{10}$/.test(value)) {
      callback(new Error("输入内容必须为10位数字！"));
    } else {
      callback();
    }
  }, // 邮箱合法验证
  isEmail: (rule, value, callback) => {
    if (value !== ""&&value !== null &&value !== undefined &&
      !/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(
        value
      )
    ) {
      callback(new Error("请正确填写您的email!"));
    } else {
      callback();
    }
  },
  // 数字
  isNumber: (rule, value, callback) => {
    if (!/^100$|^(\d|[1-9]\d)(\.\d{1,2})*$/.test(value)) {
      callback(new Error("内容必须为(0~100)的数字（可保留到2位小数)"));
    } else {
      callback();
    }
  },
  // 数字
  examNumber: (rule, value, callback) => {
    if (value !== ""&&value !== null &&value !== undefined && !/^([1-4][0-9]{2}|500|[1-9]?[0-9])$/.test(value)) {
      callback(new Error("请输入0~500的整数"));
    } else {
      callback();
    }
  },
  // 数字0~8位可为空，如果输入多位，第一位不能为0
  isNumber8: (rule, value, callback) => {
    if (value != ""&&!/^(0|[1-9]\d{0,7})(\.\d{1,2})?$/.test(value)) {

      callback(new Error("最多8位整数(可保留2位小数)"));
    } else {
      callback();
    }
  },
   // 数字0~8位可为空，如果输入多位，第一位不能为0
  isNumber10: (rule, value, callback) => {
    if (value != ""&&!/^(0|[1-9]\d{0,9})(\.\d{1,2})?$/.test(value)) {
      callback(new Error("最多10位整数(可保留2位小数)"));
    } else {
      callback();
    }
  },

   // 数字0~10位，如果输入多位，第一位不能为0
   isNumber10ForRequire: (rule, value, callback) => {
    if (value != ""&&!/^(0|[1-9]\d{0,9})(\.\d{1,2})?$/.test(value)) {
      callback(new Error("最多10位整数(可保留2位小数)"));
    }else if (value == "" || value == null) {
      callback(new Error("数字不能为空"));
    } else {
      callback();
    }
  },
  // 正数
  isPositive: (rule, value, callback) => {
    if (!/^[0-9]+([.]{1}[0-9]{1,2})?$/.test(value)) {
      callback(new Error("输入必须为正数"));
    } else if (value === null || value === "") {
      callback(new Error("输入必须为正数"));
    } else {
      callback();
    }
  } /**
   * 验证税号
   * 15或者17或者18或者20位字母、数字组成
   * @param obj
   * @returns {Boolean}
   */,
  checkTax: (rule, value, callback) => {
    if (
      /^[A-Z0-9]{15}$|^[A-Z0-9]{17}$|^[A-Z0-9]{18}$|^[A-Z0-9]{20}$/.test(value)
    ) {
      callback();
    } else {
      callback(new Error("请输入正确的税号"));
    }
  }, // 电信系统人员编号合法验证  字母和数字的组合  最长10位  /^[0-9]{8,10}$/
  isUserCode: (rule, value, callback) => {
    if (value === "" || value === null || value === undefined){
       callback();
     }else{
    if (!/^([a-z]|[A-Z]|[0-9]){1,10}$/.test(value)) {
      callback(new Error("数字、字母均可,最长10位!"));
      } else {
       callback();
     }
    }
  }, // 账户合法验证
  isComAccount: (rule, value, callback) => {
    if (!/^[0-9]{15}$/.test(value)) {
      callback(new Error("请输入正确的账号!"));
    } else {
      callback();
    }
  }, //险种基数为1位到8位数字，小数点后最多两位
  isInsurBaseRule: (rule, value, callback) => {
    if (!/^(0|[1-9]\d{0,7})(\.\d{1,2})?$/.test(value)) {
      callback(new Error("请输入合法的基数!"));
    } else {
      callback();
    }
  }, //缴纳比为百分数，最多两位小数点
  isPercent: (rule, value, callback) => {
   if (value) {
      if (!/^(100|(([1-9]\d|\d)(\.\d{1,2})?))$/.test(value)) {
        callback(new Error("请输入合法的缴纳比!"));
      } else {
        callback();
      }
    }else{
     callback();
   }
  }, //后台对接口重复验证
  // 验证资质证书重名
  validRepCer: (rule, value, callback) => {
    let certifyId = store.state.detailCertifyId || ""; //资质证书id
    var userId = store.state.idNumberUserId;
    ins
      .vaildCTrl("/app-hr/api/interface/checkCertifyName", {
        certifyName: value,
        owerUserId: userId,
        certifyId: certifyId
      })
      .then(res => {
        if (res.data.checkState == "1") {
          callback(new Error(res.msg));
        } else {
          callback();
        }
      })
      .catch(err => {
        console.log(err);
      });
  }, // 身份证重复
  validRepId: (rule, value, callback) => {
    let userId = store.state.idNumberUserId;
    ins
      .vaildCTrl("/app-hr/api/interface/checkIdNumber/", {
        idNumber: value,
        userId: userId
      })
      .then(res => {
        if (res.data == false) {
          callback(new Error(res.msg));
        } else {
          callback();
        }
      })
      .catch(err => {
        console.log(err);
      });
  }, // 电信系统人员编号重复
  validRepCard: (rule, value, callback) => {
    if (value === "" || value === null || value === undefined){
       callback();
     }else{
    var userId = store.state.idNumberUserId;
    ins
      .vaildCTrl("/app-hr/api/interface/checkTeleSysStaffNum/", {
        teleSysStaffNum: value,
        userId: userId
      })
      .then(res => {
        if (res.success == "1") {
          callback(new Error(res.msg));
        } else {
          callback();
        }
      })
      .catch(err => {
        console.log(err);
      });
    }
  }, //险种名称重复校验
  insurNameRep: (rule, value, callback) => {
    var editId = getStore("editId");
    ins
      .vaildCTrl("/app-hr/api/insurance/checkInsurName", {
        insurName: value,
        insurKindId: editId
      })
      .then(res => {
        if (res.data.checkState == "1") {
          callback(new Error(res.msg));
        } else {
          callback();
        }
      })
      .catch(err => {
        console.log(err);
      });
  }, //险种编码重复校验
  insurCodeRep: (rule, value, callback) => {
    var editId = getStore("editId");
    ins
      .vaildCTrl("app-hr/api/insurance/checkInsurCode", {
        insurCode: value,
        insurKindId: editId
      })
      .then(res => {
        if (res.data.checkState == "1") {
          callback(new Error(res.msg));
        } else {
          callback();
        }
      })
      .catch(err => {
        console.log(err);
      });
  },// 排除特殊字符验证 项目名称可以输入+号 (可以输入加号|斜杠|顿号|括号 中英文)
	isProjectName: (rule, value, callback) => {
		if (
			new RegExp(
				"[`~!@#$^&*=|':;',.<>?~！@#￥……&*‘；：”“'。，？%———— ]"
			).test(value)
		) {
			callback(new Error("内容不能出现特殊字符!"));
		} else {
			callback();
		}
	},
};
